package com.shopoo.oauth.wap;

import javax.annotation.Resource;

import com.google.gson.Gson;
import com.shopoo.dto.PageResponse;
import com.shopoo.dto.Response;
import com.shopoo.oauth.api.RoleService;
import com.shopoo.oauth.dto.cqe.RoleAddCmd;
import com.shopoo.oauth.dto.cqe.RolePowerUpdateCmd;
import com.shopoo.oauth.dto.cqe.RoleQuery;
import com.shopoo.oauth.dto.cqe.RoleUpdateCmd;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description 角色服务API
 * @Date 2020/11/20 3:08 下午
 * @Author <a href="mailto:android_li@sina.cn">Joe</a>
 **/
@Api(tags = "角色服务API")
@RestController
@RequestMapping("roles")
@Slf4j
public class RoleController {

    @Resource
    private RoleService roleService;

    /**
     * @Description 新增一个角色信息
     *
     * @Param [oauthRole]
     * @Return com.suntak.exception.model.Response
     * @Date 3:19 下午 2020/12/2
     * @Author <a href="mailto:android_li@sina.cn">Joe</a>
     */
    @ApiOperation("新增一个角色信息")
    @PostMapping
    public Response save(@Validated @RequestBody RoleAddCmd roleAddCmd) {
        log.info("添加角色：{}", new Gson().toJson(roleAddCmd));
        return roleService.save(roleAddCmd);
    }

    @ApiOperation("删除指定菜单的信息")
    @DeleteMapping("/role/{roleId}")
    public Response delete(@PathVariable("roleId") Long roleId) {
        return roleService.delete(roleId);
    }

    /**
     * @Description 更新指定角色的信息
     *
     * @Param [oauthRole]
     * @Return com.suntak.exception.model.Response
     * @Date 10:46 下午 2020/11/17
     * @Author <a href="mailto:android_li@sina.cn">Joe</a>
     */
    @ApiOperation("更新指定角色的信息")
    @PutMapping("/update")
    public Response update(@Validated @RequestBody RoleUpdateCmd roleUpdateCmd) {
        log.info("更新角色信息：{}", new Gson().toJson(roleUpdateCmd));
        return roleService.update(roleUpdateCmd);
    }

    /**
     * @Description: 更新指定角色的权限信息
     * @Author: limy66
     * @Date:   2021/3/12 16:26
     * @Param:  [user, rolePowerReq]
     * @Return: com.bngrp.common.entity.Response
     */
    @ApiOperation("更新指定角色的权限信息")
    @PutMapping("/update/menus")
    public Response updatePower(@Validated @RequestBody RolePowerUpdateCmd rolePowerUpdateCmd) {
        log.info("更新指定角色的权限信息：{}", new Gson().toJson(rolePowerUpdateCmd));
        return roleService.updatePower(rolePowerUpdateCmd);
    }

    /**
     * @Description 查询角色
     *
     * @Param [roleReq]
     * @Return com.suntak.exception.model.Response
     * @Date 3:16 下午 2020/11/20
     * @Author <a href="mailto:android_li@sina.cn">Joe</a>
     */
    @ApiOperation("分页查询角色信息")
    @GetMapping("/list")
    public PageResponse findByConditions(@Validated RoleQuery roleQuery) {
        return roleService.findByConditions(roleQuery);
    }

    /**
     * @Description: 根据角色ID查找角色拥有权限的菜单
     * @Author: limy66
     * @Date:   2021/3/12 16:24
     * @Param:  [roleId]
     * @Return: com.bngrp.common.entity.Response
     */
    @ApiOperation("根据角色ID查找角色拥有权限的菜单")
    @GetMapping("/menus/{roleId}")
    public Response findByRoleId(@PathVariable("roleId") Long roleId) {
        return roleService.findByRoleId(roleId);
    }


    /**
     * @Description 根据用户ID查找角色信息
     *
     * @Param [userId]
     * @Return com.suntak.exception.model.Response
     * @Date 1:15 下午 2020/12/2
     * @Author <a href="mailto:android_li@sina.cn">Joe</a>
     */
    @ApiOperation("根据用户ID查找角色信息")
    @GetMapping("/user/{userId}")
    public Response findRolesByUserId(@PathVariable("userId") String userId) {
        return roleService.findByUserId(userId);
    }
}
